package com.hengpeng.itfints.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.SystemConfig;
import com.hengpeng.itfints.service.ServiceFactory;
import com.hengpeng.itfints.service.SystemConfigService;

/**
 * 系统参数服务接口实现
 * @author lanmingming
 */

@Service
@Lazy
public class SystemConfigServiceImpl implements SystemConfigService
{

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<SystemConfig>> create(List<SystemConfig> objs)
	{
		List<SystemConfig> sysConfigs = new ArrayList<SystemConfig>();
		for (SystemConfig systemConfig : objs)
		{
			systemConfig = serviceFactory.getPersistService().save(systemConfig);
			sysConfigs.add(systemConfig);
		}
		return new ServiceResult<List<SystemConfig>>(sysConfigs);
	}

	@Override
	public ServiceResult<List<SystemConfig>> findAll()
	{
		List<SystemConfig> syscConfigs = serviceFactory.getPersistService().list(SystemConfig.class);
		return new ServiceResult<List<SystemConfig>>(syscConfigs);
	}

	@Override
	public ServiceResult<Long> findSysConfigCount(String id, String description, String value)
	{
		DynamicQuery pagination = new DynamicQuery(SystemConfig.class);
		if (id != null && id.trim().length() > 0)
			pagination.like("id", "%" + id + "%");
		if (description != null && description.trim().length() > 0)
			pagination.like("description", "%" + description + "%");
		if (value != null && value.trim().length() > 0)
			pagination.like("value", "%" + value + "%");
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(pagination, null));
	}

	@Override
	public ServiceResult<List<SystemConfig>> findSysConfigPage(String id, String description, String value,
			Integer pageIndex, Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery pagination = new DynamicQuery(SystemConfig.class);
		if (id != null && id.trim().length() > 0)
			pagination.like("id", "%" + id + "%");
		if (description != null && description.trim().length() > 0)
			pagination.like("description", "%" + description + "%");
		if (value != null && value.trim().length() > 0)
			pagination.like("value", "%" + value + "%");
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					pagination.desc(entry.getKey());
				}
				else
				{
					pagination.asc(entry.getKey());
				}
			}
		}
		return new ServiceResult<List<SystemConfig>>(serviceFactory.getPersistService().find(SystemConfig.class,
				pagination));
	}

	@Override
	public ServiceResult<SystemConfig> modfiy(SystemConfig obj)
	{
		SystemConfig sysConfig = serviceFactory.getPersistService().update(obj);
		return new ServiceResult<SystemConfig>(sysConfig);
	}

	@Override
	public ServiceResult<SystemConfig> remove(Set<Object> ids)
	{
		for (Object id : ids)
		{
			serviceFactory.getPersistService().delete(SystemConfig.class, id);
		}
		return new ServiceResult<SystemConfig>();
	}

	@Override
	public ServiceResult<SystemConfig> create(SystemConfig obj)
	{
		SystemConfig sysconfig = serviceFactory.getPersistService().save(obj);
		return new ServiceResult<SystemConfig>(sysconfig);
	}

	@Override
	public ServiceResult<SystemConfig> findById(String id)
	{
		SystemConfig sysconfig = serviceFactory.getPersistService().get(SystemConfig.class, id);
		return new ServiceResult<SystemConfig>(sysconfig);
	}
}
